package com.sample.tab;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
//import android.view.View.OnTouchListener;
import android.view.View;
import android.widget.TextView;

public class BouncingView extends View /*implements OnTouchListener*/{
	private Ball ball1;
	private Paint paint1;
	private Platform platform1=new Platform();
	private Rect rect;
	private TextView text;	
	private boolean isVisible=false;
	private static int score=0;

	public BouncingView(Context context) {
		super(context);
		setFocusable(true);
		ball1= new Ball(/*context, R.drawable.ball*/);
		setFocusableInTouchMode(true);
		//this.setOnTouchListener(this);
	}
    public BouncingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFocusable(true);
		ball1= new Ball(/*context, R.drawable.ball*/);
		setFocusableInTouchMode(true);
        }
@Override
public void onDraw(Canvas c){
int X2=platform1.getX()+platform1.getLength();
rect=new Rect(platform1.getX(), 710, X2, 730);	
ball1.move(ball1.getxVel(),ball1.getyVel(),ball1);	
update();
paint1= new Paint();
paint1.setColor(Color.BLUE);
paint1.setAntiAlias(true);
c.drawCircle(ball1.getX(), ball1.getY(), 25, paint1);
c.drawRect(rect, paint1);
//c.drawBitmap(ball1.getImage(), ball1.getX(), ball1.getY(), null);
invalidate();
}
/*public boolean onTouch(View v, MotionEvent event) {
	ball1.setX((int)event.getX());
	ball1.setY((int)event.getY());
	return true;
}*/
public boolean onKeyDown(int keyCode, KeyEvent msg) {
	  if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && platform1.getX()>= 20) {
		  platform1.move(-20); 
      }

      if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && platform1.getX()<= 460) {
    	  platform1.move(20); 
          }
      if (keyCode == KeyEvent.KEYCODE_SPACE && isVisible == true) {
    	  isVisible=false;
    	  reset();
          }
	return true;
}
private void update(){
collisionCheck();	
if (ball1.getY()>750){	
text.setVisibility(View.VISIBLE);
isVisible=true;
}
}
private void collisionCheck(){
int X2=platform1.getX()+platform1.getLength();
if (platform1.getX()<ball1.getX() && ball1.getX()<X2 && ball1.getY()>680){
int newVel=ball1.getyVel()*-1;	
ball1.setyVel(newVel);	
}	
}
public void setTextView(TextView tv){
text=tv;	
text.setVisibility(View.INVISIBLE);

}

private void reset(){
ball1.setX(50);
ball1.setY(50);
platform1.setX(200);
}

}
